//
//  SwitchCell.swift
//  YueChat
//
//  Created by IMac on 2024/9/14.
//

import UIKit
import RxSwift

enum SwitchCellType {
    case AllowTemporaryType
    case BlackType
    case TopType
    case NoticeType
    case SaveGroup
    case ShowName
    case AllMute
}
    

class SwitchCell: UITableViewCell {
    let disposeBag = DisposeBag()
    var topSwitchBlock: ((Bool) -> Void)?
    var type: SwitchCellType!
    var userInfo = DMCCUser()
    var con = DMCCConversation()
    var group = DMCCGroup()
    @IBOutlet weak var sw: UISwitch!
    @IBOutlet weak var titleLab: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.selectionStyle = .none
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    
    @IBAction func switchClick(_ sender: Any) {

        
        self.sw.rx.value
            .subscribe(onNext: { [self] (value) in
                
                if type == .AllowTemporaryType {
                    Task {
                        let res = await gosnUser.setAllowTemporaryChat(value ? "yes" : "no")
                        
                        await MainActor.run {
                            
                            if res {
                                Task {
                                    gosnUser.fetchMyInfo
                                }
                            }
                        }
                        
                    }
                    
                }else if type == .BlackType {
                    Task {
                        let res = await gosnFriend.blackFriend(self.userInfo.userId, value)
                        if res {
                            
                        }
                    }
   
                }else if type == .TopType {
                    self.con.isTop = value
                    gosnConversation.setTop(self.con.targetId, value)
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: kUpdateConversation), object: nil)
                    
                }else if type == .NoticeType {
                    self.con.isSilent = value
//                    if DMCCIMService.getInstance().setConversationSilent(self.con) {
                    gosnConversation.setSilent(self.con.targetId, value)
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: kUpdateConversation), object: nil)
                    
                }else if type == .SaveGroup {
//                    let status = value.hashValue
//                    let status = (value == true) ? 1 : 0
                    
                    Task {
                        let res = await gosnGroup.saveGroup(self.con.targetId, value)
                        if res {
                            
                        }
                    }
                    
                    
//                    DMCCIMService.getInstance().saveGroup(con?.targetId, status: value) { [self] isSuccess, json, err in
//                        let group = gosnGroup.getGroupInfo(con.targetId)!
//                        DMCCIMService.getInstance().updateGroupFav(group, fav: value)
//                    }
                    
                }else if type == .ShowName {
                    if gosnGroup.updateShowAlias(con.targetId, value) {
                        NotificationCenter.default.post(name: NSNotification.Name(rawValue: kGroupUpdateChanged), object: con)
                    }
                }else if type == .AllMute {
                    Task {
                        let res = await gosnGroup.muteGroup(group.groupId, value)
                        if res {
                            
                        }
                    }
                }
                
                
            })
            .disposed(by: disposeBag)
        
        
        
    }
    
    func fetchData(completionHandler: @escaping (Bool) -> Void) {
        // 模拟数据获取
//        let data = "Sample Data"
        // 调用闭包
//        completionHandler(data)
    }

//    // 调用闭包
//    switchTop(true)
    
}
